#include<func.h>
#include"threadPool.h"
#include"head.h"
#include "token.h"
#include "account.h"
int exitPipe[2];
void sigFunc(int signum){
    printf("signum is comming!\n");
    write(exitPipe[1],"1",1);
}
int main(int argc,char* argv[]){
    //./server 192.168.4.128 1234 4
    ARGS_CHECK(argc,4);
    pipe(exitPipe);
    if(fork() != 0){
        //父进程
        close(exitPipe[0]);
        signal(SIGUSR1,sigFunc);
        wait(NULL);
        exit(0);
    }
    close(exitPipe[1]);
    int workerNum =atoi(argv[3]);
    threadPool_t threadPool;
    threadPoolInit(&threadPool,workerNum);
    int sockFd;
    makeWorker(&threadPool);//线程建立
    tcpInit(argv[1],argv[2],&sockFd);
    int epfd = epoll_create(10);
    epollAdd(sockFd,epfd);
    epollAdd(exitPipe[0],epfd);
    struct epoll_event readyList[2];
    //存储每个客户端的信息
    clientMsg_t client[100] = {0};
    int count;
    //设置一个固定的初始化盐值
    char *tokenSalt = (char*)calloc(20,sizeof(char));
    randSalt(&tokenSalt);
    //输出一个固定盐值，用于测试
    printf("tokenSalt: %s\n",tokenSalt);

    while(1){
        int readyNum = epoll_wait(epfd,readyList,2,-1);
        puts("epoll ready");
        for(int i=0; i<readyNum;i++){
            if(readyList[i].data.fd == sockFd){
                int netFd = accept(sockFd,NULL,NULL);
                char token[300] = {0};
                int dataLength;
                puts("new sockFd");
                //接收客户端发送过来的token密文
                bzero(token,sizeof(token));
                recv(netFd,&dataLength,sizeof(dataLength),0);
                recv(netFd,token,dataLength,0);
                printf("recv token : %s\n",token);
                //第一次连接则登录并创建token密文
                if(strcmp(token,"first") == 0){
                    firstConnect(netFd,&client[count],tokenSalt);
                    epollAdd(netFd,epfd);
                    printf("netFd = %d\n",netFd);
                    count++;
                }
                else{//第二次连接则将转到线程中，用于上传以及下载
                    pthread_mutex_lock(&threadPool.mutex);
                    enQueue(&threadPool.taskQueue,netFd,token,tokenSalt);//创建一个工作队列
                    puts("new task");
                    pthread_cond_signal(&threadPool.cond);
                    pthread_mutex_unlock(&threadPool.mutex);
                }

            }
            else if(readyList[i].data.fd == exitPipe[0]){
                //for(int j=0;j<workerNum;j++){
                //    pthread_cancel(threadPool.tidArr[j]);
                //}
                puts("I am going to die!");
                threadPool.isThreadPoolRunning = 0;
                pthread_cond_broadcast(&threadPool.cond);
                for(int j=0;j<workerNum;j++){
                    pthread_join(threadPool.tidArr[j],NULL);
                }
                puts("Exit");
                exit(0);
            }
            else{
                for(int j = 0;j<count;j++){
                    if(readyList[i].data.fd == client[j].netFd){
                        puts("new commond");
                        shortCommond(client[j].netFd,client[j],epfd);
                    }
                }
            }
            
        }
    }
    free(tokenSalt);
}